﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using Hont.AStar;

public class Example5_StartPathfindingLessGCAlloc : MonoBehaviour
{
    Vector3[] mCachedPath;
    Position[] mCachePosition;

    Stopwatch mStopwatch;

    public HontAStarUnity astar;
    public float debug_stopwatchMs;
    public bool drawHelperGizmos = false;


    void OnEnable()
    {
        if (astar == null) return;

        mCachedPath = new Vector3[100];
        mCachePosition = new Position[100];

        mStopwatch = new Stopwatch();
    }

    void Update()
    {
        var aVector = default(Vector3);
        var bVector = default(Vector3);

        aVector.x = UnityEngine.Random.Range(astar.LocalBounds.min.x, astar.LocalBounds.max.x);
        aVector.y = UnityEngine.Random.Range(astar.LocalBounds.min.y, astar.LocalBounds.max.y);
        aVector.z = UnityEngine.Random.Range(astar.LocalBounds.min.z, astar.LocalBounds.max.z);

        bVector.x = UnityEngine.Random.Range(astar.LocalBounds.min.x, astar.LocalBounds.max.x);
        bVector.y = UnityEngine.Random.Range(astar.LocalBounds.min.y, astar.LocalBounds.max.y);
        bVector.z = UnityEngine.Random.Range(astar.LocalBounds.min.z, astar.LocalBounds.max.z);

        aVector = astar.transform.TransformPoint(aVector);
        bVector = astar.transform.TransformPoint(bVector);

        mStopwatch.Reset();
        mStopwatch.Start();
        var length = astar.StartPathfinding_NonAlloc(aVector, bVector, ref mCachePosition, ref mCachedPath);
        mStopwatch.Stop();

        debug_stopwatchMs = mStopwatch.ElapsedMilliseconds;

        if (length > -1 && drawHelperGizmos)
        {
            var tempList = new List<Vector3>();
            for (int j = 0; j < length; j++)
            {
                tempList.Add(mCachedPath[j]);
            }

            HontAStarUnityHelper.DebugPathfindingPath(tempList.ToArray());
        }
    }
}
